package com.ice_col.oauthdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.api.Baidu;
import com.baidu.api.BaiduDialog;
import com.baidu.api.BaiduDialogError;
import com.baidu.api.BaiduException;

public class MainActivity extends AppCompatActivity {
    public static String BAIDU_KEY="qPtEEqzR5ek4NEL68m08iZBS";
    public static String USER_INFO_URL="https://openapi.baidu.com/rest/2.0/passport/users/getLoggedInUser";

    private Button bt_Oauth;
    private TextView tv_shwoInfo;


    private Button bt_getUserInfo;
    private TextView tv_showUserInfo;

    private String token;
    private Baidu baidu ;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView(){
        //Authorization
        bt_Oauth=(Button) findViewById(R.id.oauth_bt_baiduOAuth);
        tv_shwoInfo=(TextView) findViewById(R.id.oauth_tv_showInfo);
        bt_Oauth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                baiduOAuth();
            }
        });

        //get user info
        bt_getUserInfo=(Button) findViewById(R.id.oauth_bt_getUserInfo);
        tv_showUserInfo=(TextView)findViewById(R.id.oauth_tv_showUserInfo);
        bt_getUserInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "test", Toast.LENGTH_LONG).show();
                if (token == null) {
                    Toast.makeText(MainActivity.this, "token is null", Toast.LENGTH_LONG).show();
                } else {
                    getUserInfo();
                }
            }
        });

    }


    private void getUserInfo(){
       new Thread(){
           public void run(){
               if(baidu==null){
                   baidu=new Baidu(BAIDU_KEY, MainActivity.this);
               }
               String response=null;
               try {
                   response=baidu.request(USER_INFO_URL+"?access_token="+token, null, "POST");
                   System.out.println("response");
               }catch (Exception e){
                   e.printStackTrace();
               }
               showUserInfo(response);

           }
       }.start();
    }

    private void showUserInfo(final String userInfo){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(userInfo==null){
                    Toast.makeText(MainActivity.this, "null info",Toast.LENGTH_LONG).show();
                }
                tv_showUserInfo.setText(userInfo);
            }
        });
    }
    //Authorization
    private void baiduOAuth() {
        System.out.println("test21");
        baidu = new Baidu("qPtEEqzR5ek4NEL68m08iZBS", this);

        baidu.authorize(this, true, true, new BaiduDialog.BaiduDialogListener() {
            @Override
            public void onComplete(Bundle bundle) {
                token=baidu.getAccessToken();
                reflahUI("complete:" + token);

            }

            @Override
            public void onBaiduException(BaiduException e) {
                reflahUI("onBaiduException");
            }

            @Override
            public void onError(BaiduDialogError baiduDialogError) {
                reflahUI("onError");
            }

            @Override
            public void onCancel() {
                reflahUI("onCancel");
            }
        });
    }

    private void reflahUI(final String s){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv_shwoInfo.setText(s);
                System.out.println("test31");
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
